<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRoles"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="success"
                    @click="hAssign(scope.row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="doEdit(scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="doDelRole(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :total="total"
                :current-page="page"
                :page-sizes="[1, 2, 3, 4]"
                :page-size="pagesize"
                layout="prev,pager,next,sizes"
                @size-change="hSizeChange"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit ? '编辑角色' : '添加角色'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="hSubmit"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限的弹层 -->
      <el-dialog
        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :visible.sync="showDialogAssign"
      >
        <assign-permission
          ref="permission"
          :cur-id="curId"
          @success="Hsuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  getRoles,
  delRoles,
  addRole,
  getRolesDetail,
  EditRole
} from '@/api/settings'
import assignPermission from './assignPermission.vue'

export default {
  components: {
    assignPermission
  },
  data() {
    return {
      curId: '',
      showDialogAssign: false,
      list: [],
      total: 0,
      page: 1,
      pagesize: 1,
      showDialog: false,
      isEdit: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    Hsuccess() {
      this.showDialogAssign = false
      this.$message.success('修改权限成功')
    },
    hAssign(id) {
      this.curId = id
      this.$nextTick(() => {
        this.$refs.permission.loadPermissionList()
      })
      this.showDialogAssign = true
    },
    async doEdit(data) {
      this.showDialog = true
      this.isEdit = true
      const res = await getRolesDetail(data.id)
      this.roleForm = res.data
    },
    async doEditRole() {
      await EditRole(this.roleForm)
      this.showDialog = false
      this.$message.success('编辑成功')
      this.$refs.roleForm.resetFields()
      this.loadRoles()
    },
    async hAddRole() {
      await addRole(this.roleForm)
      this.showDialog = false
      this.$refs.roleForm.resetFields()
      this.total++
      if (this.list.length === this.pagesize) {
        this.page = Math.ceil(this.total / this.pagesize)
      }
      this.$message.success('添加成功')
      this.loadRoles()
    },
    addRoles() {
      this.isEdit = false
      this.showDialog = true
    },
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEditRole() : this.hAddRole()
        }
      })
    },
    async doDelRole(id) {
      await delRoles(id)
      if (this.list.length === 1 && this.page > 1) {
        this.page--
      }
      this.loadRoles()
      this.$message.success('删除成功')
    },
    // 每页多少条
    hSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.loadRoles()
    },
    // 当前在那一页
    hCurrentChange(val) {
      this.page = val
      this.loadRoles()
    },
    async loadRoles() {
      const res = await getRoles({ page: this.page, pagesize: this.pagesize })
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>
